using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace CEQuery
{
    public partial class TablePanel : UserControl
    {
        public event ColumnCheckEventHandler ColumnCheck;
        protected virtual void OnColumnCheck(ColumnCheckEventArgs e)
        {
            ColumnCheck(this, e);
        }
        public Point Center
        {
            get { return new Point(Location.X + this.Width/2, Location.Y + this.Height/2); }
        }
        public bool IsTableSelected
        {
            get { return chkTable.Checked; }
            set { chkTable.Checked = value; }
        }
        
        public string ColumnSelected
        {
            get { return columnList.SelectedItem.ToString(); }
            
        }

        private List<string> columns;

        public List<string> Columns
        {
            get { return columns; }
            set { columns = value; }
        }
        private string tableName;

        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }
	
        public TablePanel()
        {
            InitializeComponent();
        }

        private void TablePanel_Load(object sender, EventArgs e)
        {
            chkTable.Text = tableName;
            foreach(string column in columns)
            {
                columnList.Items.Add(column, false);
            }
        }

        public void ClearSelection()
        {
            chkTable.Checked = false;
            foreach (int index in columnList.CheckedIndices)
            {
                columnList.SetItemChecked(index, false);
            }
        }

        private void columnList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ColumnCheckEventArgs columnCheckArgs = new ColumnCheckEventArgs();
            columnCheckArgs.TableName = this.tableName;
            columnCheckArgs.ColumnName = columnList.SelectedItem.ToString();
            columnCheckArgs.IsColumnChecked = columnList.GetItemChecked(columnList.SelectedIndex);
            OnColumnCheck(columnCheckArgs);
        }
    }
    public delegate void ColumnCheckEventHandler(object sender, ColumnCheckEventArgs e);
    public class ColumnCheckEventArgs : EventArgs
    {
        public string TableName { get; set; }
        public string ColumnName { get; set; }
        public bool IsColumnChecked { get; set; }        
    }
}
